<?php
class ArticleUserForm extends CFormModel{
    public $username;
    public $role;
    public $article;

    public function rules(){
        return array(
            array('username, role', 'required'),
            array('username', 'verify'),
            array('username', 'exist', 'className'=>'User'),
        );
    }

    public function verify($attribute, $params){
        if (!$this->hasErrors()){//authenticate when no other input errors are present
            $user = User::model()->findByAttributes(array('username'=>$this->username));
            if ($this->article->isUserInArticle($user)){
                $this->addError('username', 'This user has already been added to the topic.');
            }
            else{
                $this->article->associateUserToArticle($user);
                $this->article->associateUserToRole($this->role, $user->id);

                $auth = Yii::app()->authManager;
//                $bizRule = 'return isset($params["article"]) &&
//                    $params["article"]->isUserInRole("'.$this->role.'");';
//                $auth->assign($this->role, $user->id, $bizRule);
            }
        }
    }
}

?>
